package com.yc.springcloudindex.web;

import com.yc.springcloudindex.bean.User;
import com.yc.springcloudindex.web.feign.OrderAction;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;



@RestController
public class IndexAction {


    @RequestMapping("index")
    public String index(HttpServletRequest request){
        int port = request.getServerPort();
        return "Index:" + port;
    }

    @Resource
    RestTemplate restTemplate;

    @RequestMapping("order")
    public String order(){
        return restTemplate.getForObject(
                "http://spring-cloud-order/order",
                String.class
        );
    }


    @RequestMapping("user")
    public String user(){
        return restTemplate.getForObject(
                "http://spring-cloud-user/user",
                String.class
        );
    }

    @Resource
    OrderAction orderAction;

    @RequestMapping("orderForFeign")
    public String orderForFeign(){
        return orderAction.order();
    }


    @RequestMapping("sayHello1")
    public String sayHello1(String name){
        return orderAction.hello(name);
    }

    @RequestMapping("sayHello2/{name}")
    public String sayHello2(@PathVariable("name") String name){
        return orderAction.hello1(name);
    }


    @RequestMapping("showMe")
    public User showMe(User user,String newName){
        return orderAction.showMe(user,newName);
    }


}
